package com.bw.controller;

import com.bw.bean.Student;
import com.bw.server.UserServer;
import com.bw.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class UserController {


    @Autowired //他就是到内存中找对象
    UserServer userServer;

    @GetMapping("/findAll")
    public List<Map> findAll(){
        return userServer.findAll();
    }


    @GetMapping("/findById")
    public Student findById(Integer id){
        return userServer.findById(id);
    }

    @RequestMapping("/getUv")
    public String getUv(@RequestParam(defaultValue = "1") Integer dt){

        if (dt == 1){
            dt = DateUtil.now();
        }


        List<Map> list = userServer.findTrafficUv(dt);

        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\":");
        ArrayList<String> keyList = new ArrayList<>();
        ArrayList<Integer> valueList = new ArrayList<>();
        for (Map map : list) {
            keyList.add(map.get("ch").toString());
            valueList.add(Integer.valueOf(map.get("uv").toString()));
        }

        stringBuffer.append("[\"");
        stringBuffer.append(StringUtils.join(keyList,"\",\""));
        stringBuffer.append("\"]");

        stringBuffer.append(",\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"手机品牌\",\n" +
                "        \"data\":");

        stringBuffer.append(valueList.toString());

        stringBuffer.append("}\n" +
                "    ]\n" +
                "  }\n" +
                "}");


        return stringBuffer.toString();
    }

    @RequestMapping("/test")
    public String test(){
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": [\n" +
                "      \"苹果111\",\n" +
                "      \"三星\",\n" +
                "      \"华为\",\n" +
                "      \"oppo\",\n" +
                "      \"vivo\",\n" +
                "      \"小米13\"\n" +
                "    ],\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"手机品牌\",\n" +
                "        \"data\": [\n" +
                "          9901,\n" +
                "          6094,\n" +
                "          6181,\n" +
                "          9928,\n" +
                "          6784,\n" +
                "          7349\n" +
                "        ]\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";
    }






}
